package com.peas.platform.module.icims.budget.archives.service.impl;

import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.sys.enums.CommonEnableDisable;
import com.peas.platform.module.icims.budget.archives.domain.BudgetKpiDomain;
import com.peas.platform.module.icims.budget.archives.dto.BudgetKpiDto;
import com.peas.platform.module.icims.budget.archives.enums.KpiLevel;
import com.peas.platform.module.icims.budget.archives.enums.KpiSource;
import com.peas.platform.module.icims.budget.archives.service.BudgetKpiService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class BudgetKpiServiceImpl extends BaseServiceImpl<BudgetKpiDto> implements BudgetKpiService {

    @Resource
    private BudgetKpiDomain budgetKpiDomain;

    @Override
    public BaseDomain<BudgetKpiDto, ? extends BasePo> getDomain() {
        return budgetKpiDomain;
    }

    @Transactional
    @Override
    public BudgetKpiDto addBudgetKpi(KpiSource source, String kpiName, Long parentId, Integer weight) {
        if (parentId == null)
            return null;
        BudgetKpiDto parent = this.getById(parentId);
        if (parent == null)
            return null;
        BudgetKpiDto dto = new BudgetKpiDto();
        dto.setSource(source.getKey());
        dto.setLevel(Integer.valueOf(KpiLevel.three.getKey()));
        dto.setName(kpiName);
        dto.setWeight(weight);
        dto.setParentId(parentId);
        dto.setState(CommonEnableDisable.enabled.getKey());
        int ret = this.save(dto);
        Assert.isTrue(ret > 0, "保存失败");
        return dto;
    }

}
